/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-18     Meco Man     The first version
 */
#if 1
#include <lvgl.h>
#include <rtconfig.h> 

#define MY_DISP_HOR_RES     LCD_W

#if defined(LCD_USING_TFT_ST77903)
    #define DISP_BUFFER_LINES   (LCD_H/3) /*@tips: 对于ST77903，需要设置为1/3或者1/5,因为RAM不够*/
#else
    #define DISP_BUFFER_LINES     LCD_H
#endif

/*A static or global variable to store the buffers*/
static lv_disp_draw_buf_t disp_buf;

/*Descriptor of a display driver*/
static lv_disp_drv_t disp_drv;

/*Static or global buffer(s). The second buffer is optional*/
// static lv_color_t buf_1[MY_DISP_HOR_RES * DISP_BUFFER_LINES];

static lv_disp_draw_buf_t draw_buf_dsc_2;
static lv_color_t buf_2_1[MY_DISP_HOR_RES * DISP_BUFFER_LINES] __attribute__((aligned(64))) ;     /*A buffer for 10 rows*/
static lv_color_t buf_2_2[MY_DISP_HOR_RES * DISP_BUFFER_LINES] __attribute__((aligned(64))) ;     /*An other buffer for 10 rows*/


/*Flush the content of the internal buffer the specific area on the display
 *You can use DMA or any hardware acceleration to do this operation in the background but
 *'lv_disp_flush_ready()' has to be called when finished.*/
static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{
    /* color_p is a buffer pointer; the buffer is provided by LVGL */
    #if defined(LCD_USING_TFT_ST77903)
        extern void lcdqspi_wait_te(void);
        lcdqspi_wait_te();
    #endif

    extern void lcd_fill_pixels(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint8_t *pcolor);
    lcd_fill_pixels(area->x1, area->y1, area->x2, area->y2, (uint8_t *)color_p);

    /*IMPORTANT!!!
     *Inform the graphics library that you are ready with the flushing*/
    lv_disp_flush_ready(disp_drv);
}

void lv_port_disp_init(void)
{
    /*Initialize `disp_buf` with the buffer(s). With only one buffer use NULL instead buf_2 */
    // lv_disp_draw_buf_init(&disp_buf, buf_1, RT_NULL, MY_DISP_HOR_RES * DISP_BUFFER_LINES);
    lv_disp_draw_buf_init(&draw_buf_dsc_2, buf_2_1, buf_2_2, MY_DISP_HOR_RES * DISP_BUFFER_LINES); 

    lv_disp_drv_init(&disp_drv); /*Basic initialization*/

    /*Set the resolution of the display*/
    disp_drv.hor_res = LCD_W;
    disp_drv.ver_res = LCD_H;

    /*Set a display buffer*/
    // disp_drv.draw_buf = &disp_buf;
    disp_drv.draw_buf = &draw_buf_dsc_2;

    /*Used to copy the buffer's content to the display*/
    disp_drv.flush_cb = disp_flush;

    /*Finally register the driver*/
    lv_disp_drv_register(&disp_drv);
}

#endif
